#include "datainit.h"

DataInit::DataInit()
{

}

void DataInit::run()
{
    QFile file("config.ini");
     if(!file.open(QIODevice::ReadOnly)){
         emit InitFinished();
         return;
     }
    QString md5=getHtml("https://gitee.com/CharlinChen/GISChoose/raw/master/version/MD5.txt");
   if(md5 == ""){
       emit InitFinished();
       return;
   }
    QStringList md5list = md5.split("\n");
    metadata_B_md5=md5list[0].split("=")[1];
    metadata_G_md5=md5list[1].split("=")[1];
    Ud_B_md5=md5list[2].split("=")[1];
    Ud_G_md5=md5list[3].split("=")[1];
    Ue_B_md5=md5list[4].split("=")[1];
    Ue_G_md5=md5list[5].split("=")[1];
    lanuch_B_md5=md5list[6].split("=")[1];
    lanuch_G_md5=md5list[7].split("=")[1];
    qDebug()<<Ue_G_md5;
    type=file.readLine();
    QString line="NULL";
    QStringList  list2;
    while(line!=""){
        line=file.readLine().trimmed();
        list2 = line.split("=");
        if(list2[0]=="cps")cps=list2[1];
        else if(list2[0]=="channel")channel=list2[1];
        else if(list2[0]=="sub_channel")sub_channel=list2[1];
        else if(list2[0]=="game_install_path")game_install_path=list2[1];
        else if(list2[0]=="is_user_update")is_user_update=list2[1];
        else if(list2[0]=="new_version")new_version=list2[1];
        else if(list2[0]=="game_dynamic_bg_name")game_dynamic_bg_name=list2[1];
        else if(list2[0]=="is_first_exit")is_first_exit=list2[1];
        else if(list2[0]=="exit_type")exit_type=list2[1];
        else if(list2[0]=="is_auto_update")is_auto_update=list2[1];
        else if(list2[0]=="game_dynamic_bg_md5")game_dynamic_bg_md5=list2[1];
    }
    qDebug()<<sub_channel;
    file.close();
    game_install_path=Unicode2string(game_install_path);
    qDebug()<<game_install_path;
    if(CheckSysMD5())emit InitFinished();
    else emit InitFail();
}

QString DataInit::getHtml(QString URLSTR)
{
        QUrl url(URLSTR);
        QNetworkAccessManager manager;
        QEventLoop loop;
        QNetworkReply *reply = manager.get(QNetworkRequest(url));
        QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
        loop.exec();
        QString code = reply->readAll();
        return code;
}
QString DataInit::CheckFileMD5(QString path){
    QFileInfo fileInfo(path);
       if(!fileInfo.isFile())return "";
       else{
            QFile theFile(path);
            theFile.open(QIODevice::ReadOnly);
            QByteArray ba = QCryptographicHash::hash(theFile.readAll(), QCryptographicHash::Md5);
            theFile.close();
            return ba.toHex().constData();
    }
}
bool DataInit::CheckSysMD5(){
    QString md5;
    if(cps=="mihoyo" || cps=="pcadbdpz"){
        md5=CheckFileMD5(game_install_path+"/YuanShen_Data/Managed/Metadata/global-metadata.dat.b");
        if(md5!=metadata_B_md5)return false;
        md5=CheckFileMD5(game_install_path+"/YuanShen_Data/Native/UserAssembly.dll.b");
        if(md5!=Ud_B_md5)return false;
        md5=CheckFileMD5(game_install_path+"/YuanShen_Data/Native/UserAssembly.exp.b");
        if(md5!=Ue_B_md5)return false;
    }
    else if(cps=="bilibili"){
        md5=CheckFileMD5(game_install_path+"/YuanShen_Data/Managed/Metadata/global-metadata.dat.g");
        if(md5!=metadata_G_md5)return false;
        md5=CheckFileMD5(game_install_path+"/YuanShen_Data/Native/UserAssembly.dll.g");
        if(md5!=Ud_G_md5)return false;
        md5=CheckFileMD5(game_install_path+"/YuanShen_Data/Native/UserAssembly.exp.g");
        if(md5!=Ue_G_md5)return false;
    }else {
        md5=CheckFileMD5(game_install_path+"/YuanShen_Data/Managed/Metadata/global-metadata.dat.b");
        if(md5!=metadata_B_md5)return false;
        md5=CheckFileMD5(game_install_path+"/YuanShen_Data/Native/UserAssembly.dll.b");
        if(md5!=Ud_B_md5)return false;
        md5=CheckFileMD5(game_install_path+"/YuanShen_Data/Native/UserAssembly.exp.b");
        if(md5!=Ue_B_md5)return false;
        md5=CheckFileMD5(game_install_path+"/YuanShen_Data/Managed/Metadata/global-metadata.dat.g");
        if(md5!=metadata_G_md5)return false;
        md5=CheckFileMD5(game_install_path+"/YuanShen_Data/Native/UserAssembly.dll.g");
        if(md5!=Ud_G_md5)return false;
        md5=CheckFileMD5(game_install_path+"/YuanShen_Data/Native/UserAssembly.exp.g");
        if(md5!=Ue_G_md5)return false;
    }
    md5=CheckFileMD5("launcher.exe");
     if(md5==lanuch_B_md5)game_version="1.2.1";
     else if(md5==lanuch_G_md5)game_version="1.2.0";
     else game_version="1.2.0";
    return true;
}

inline bool isNumber(char ch){
    return (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f');
}
QString DataInit::Unicode2string(QString str1)
{
        QByteArray content = str1.toLatin1();
        qDebug()<<content<<content.size()<<endl;
        QString str;
        for (int i = 0; i < content.size();){
            if (content.at(i) == '\\' && content.at(i + 1) == 'x' && isNumber(content.at(i + 2)) && isNumber(content.at(i + 3)) && isNumber(content.at(i + 4)) && isNumber(content.at(i + 5)))
            {
                QChar var = content.mid(i + 2, 4).toInt(0, 16);  // 分离出一个unicode字符
                str.append(var);
                i += 6;
                //qDebug()<<i<<str<<endl;
            } else {
                QByteArray byte=content.mid(i,1);
                char *var;
                var =  byte.data(); // 分离出一个ascii字符
                str.append(var);
                i++;
                //qDebug()<<i<<str<<endl;
            }
        }
    return str;
}

